using System;
using System.Collections.Generic;
using System.ComponentModel;


namespace HumanElement.Xna.Construct.GameState.MenuItems
{
    public class NavigationLinkMenuItem : MenuItem
    {
        private string text;
        private Type backgroundScreenType;
        private Type nextScreenType;

        private const string LabelDefault = "Go to Next";

        public NavigationLinkMenuItem()
        {
            this.text = LabelDefault;
        }

        [Category("Appearance")]
        [DefaultValue(LabelDefault)]
        public string Label
        {
            get { return text; }
            set
            {
                this.text = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(null)]
        [TypeConverter(typeof(TypeOfTypeConverter<BackgroundScreen>))]
        public Type BackgroundScreen
        {
            get { return this.backgroundScreenType; }
            set
            {
                if (value != null)
                {
                    if (value.GetConstructor(System.Type.EmptyTypes) == null)
                    {
                        // No default constructor.
                        throw new ArgumentException("The specified type must have a public default constructor.", "value");
                    }
                    if (!typeof(BackgroundScreen).IsAssignableFrom(value))
                    {
                        // Not a GameScreen.
                        throw new ArgumentException(String.Format("The specified type must derive from {0}.", typeof(BackgroundScreen).FullName), "value");
                    }
                }
                this.backgroundScreenType = value;
            }
        }

        [Category("Behavior")]
        [DefaultValue(null)]
        [TypeConverter(typeof(TypeOfTypeConverter<GameScreen>))]
        public Type NextScreen
        {
            get { return nextScreenType; }
            set
            {
                if (value.GetConstructor(System.Type.EmptyTypes) == null)
                {
                    // No default constructor.
                    throw new ArgumentException("The specified type must have a public default constructor.", "value");
                }
                if (!typeof(GameScreen).IsAssignableFrom(value))
                {
                    // Not a GameScreen.
                    throw new ArgumentException(String.Format("The specified type must derive from {0}.", typeof(GameScreen).FullName), "value");
                }
                this.nextScreenType = value;
            }
        }

        public override string GetText()
        {
            return this.text;
        }

        protected internal override void OnSelected()
        {
            // Raise the selected event first
            base.OnSelected();

            // Transition to the next screen.
            if (this.nextScreenType != null)
            {
                MenuScreen menuScreen = this.Parent;
                if (menuScreen != null)
                {
                    GameScreen nextScreen = (GameScreen)Activator.CreateInstance(this.nextScreenType);
                    BackgroundScreen background = null;
                    if (this.backgroundScreenType != null)
                    {
                        background = (BackgroundScreen)Activator.CreateInstance(this.backgroundScreenType);
                    }
                    if (background != null)
                    {
                        background.IsPopup = nextScreen.IsPopup;
                        nextScreen.BackgroundScreen = background;
                    }
                    menuScreen.ScreenManager.AddScreen(nextScreen);
                }
            }
        }
    }
}
